<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>精美可折叠侧边栏</title>
    <style>
        /* 基础样式重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            display: flex;
            min-height: 100vh;
            background-color: #f5f7fa;
            color: #333;
            overflow-x: hidden;
        }

        /* 侧边栏容器样式 */
        .sidebar {
            width: 250px;
            height: 100vh;
            background: linear-gradient(135deg, #3a4f6a, #2c3e50);
            color: white;
            transition: all 0.3s ease;
            position: relative;
            box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
        }

        /* 折叠状态下的侧边栏 */
        .sidebar.collapsed {
            width: 70px;
        }

        /* 侧边栏头部样式 */
        .sidebar-header {
            padding: 20px;
            text-align: center;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            transition: all 0.3s ease;
        }

        /* 折叠状态下的头部 */
        .sidebar.collapsed .sidebar-header {
            padding: 20px 10px;
        }

        .logo {
            font-size: 1.5rem;
            font-weight: bold;
            white-space: nowrap;
            overflow: hidden;
            transition: all 0.3s ease;
        }

        .sidebar.collapsed .logo {
            font-size: 1.2rem;
        }

        /* 导航菜单样式 */
        .sidebar-menu {
            flex: 1;
            overflow-y: auto;
            padding: 10px 0;
        }

        .menu-item {
            display: flex;
            align-items: center;
            padding: 12px 20px;
            cursor: pointer;
            transition: all 0.2s ease;
            border-left: 3px solid transparent;
            white-space: nowrap;
        }

        .menu-item:hover {
            background-color: rgba(255, 255, 255, 0.1);
            border-left: 3px solid #3498db;
        }

        .menu-item.active {
            background-color: rgba(255, 255, 255, 0.15);
            border-left: 3px solid #3498db;
        }

        .menu-icon {
            font-size: 1.2rem;
            margin-right: 15px;
            min-width: 20px;
            text-align: center;
            transition: all 0.3s ease;
        }

        .sidebar.collapsed .menu-icon {
            margin-right: 0;
        }

        .menu-text {
            transition: all 0.3s ease;
            opacity: 1;
        }

        /* 折叠状态下隐藏文本 */
        .sidebar.collapsed .menu-text {
            opacity: 0;
            width: 0;
            margin-left: 0;
            display: none;
        }

        /* 侧边栏底部样式 */
        .sidebar-footer {
            padding: 15px;
            border-top: 1px solid rgba(255, 255, 255, 0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
            transition: all 0.3s ease;
        }

        .sidebar.collapsed .sidebar-footer {
            justify-content: center;
        }

        .user-info {
            display: flex;
            align-items: center;
            white-space: nowrap;
            overflow: hidden;
        }

        .sidebar.collapsed .user-info {
            display: none;
        }

        .user-avatar {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            margin-right: 10px;
            background-color: #3498db;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
        }

        .toggle-btn {
            background: none;
            border: none;
            color: white;
            font-size: 1.2rem;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .toggle-btn:hover {
            color: #3498db;
            transform: scale(1.1);
        }

        /* 主内容区域样式 */
        .main-content {
            flex: 1;
            padding: 20px;
            transition: all 0.3s ease;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }

        .page-title {
            font-size: 1.5rem;
            color: #2c3e50;
        }

        .content {
            background-color: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }

        /* 响应式调整 */
        @media (max-width: 768px) {
            .sidebar {
                position: absolute;
                z-index: 100;
                left: 0;
                transform: translateX(0);
            }

            .sidebar.collapsed {
                transform: translateX(-100%);
                width: 250px;
            }

            .sidebar.collapsed .menu-text {
                opacity: 1;
                display: block;
                width: auto;
            }

            .sidebar.collapsed .menu-icon {
                margin-right: 15px;
            }

            .sidebar.collapsed .logo {
                font-size: 1.5rem;
            }

            .sidebar.collapsed .sidebar-header {
                padding: 20px;
            }

            .sidebar.collapsed .user-info {
                display: flex;
            }

            .sidebar.collapsed .sidebar-footer {
                justify-content: space-between;
            }
        }
    </style>
</head>
<body>
    <!-- 侧边栏结构 -->
    <div class="sidebar">
        <!-- 侧边栏头部 -->
        <div class="sidebar-header">
            <div class="logo">Admin Panel</div>
        </div>
        
        <!-- 导航菜单 -->
        <div class="sidebar-menu">
            <div class="menu-item active">
                <span class="menu-icon">🏠</span>
                <span class="menu-text">仪表盘</span>
            </div>
            <div class="menu-item">
                <span class="menu-icon">📊</span>
                <span class="menu-text">数据分析</span>
            </div>
            <div class="menu-item">
                <span class="menu-icon">✉️</span>
                <span class="menu-text">消息中心</span>
            </div>
            <div class="menu-item">
                <span class="menu-icon">🛒</span>
                <span class="menu-text">产品管理</span>
            </div>
            <div class="menu-item">
                <span class="menu-icon">👥</span>
                <span class="menu-text">用户管理</span>
            </div>
            <div class="menu-item">
                <span class="menu-icon">⚙️</span>
                <span class="menu-text">系统设置</span>
            </div>
        </div>
        
        <!-- 侧边栏底部 -->
        <div class="sidebar-footer">
            <div class="user-info">
                <div class="user-avatar">A</div>
                <span>管理员</span>
            </div>
            <button class="toggle-btn" id="toggleBtn">◀</button>
        </div>
    </div>
    
    <!-- 主内容区域 -->
    <div class="main-content" id="mainContent">
        <div class="header">
            <h1 class="page-title">仪表盘</h1>
            <button class="toggle-btn mobile-toggle" id="mobileToggleBtn">☰</button>
        </div>
        <div class="content">
            <p>欢迎使用管理系统。这是一个精美的可折叠侧边栏实现。</p>
            <p>点击侧边栏底部的箭头按钮可以折叠/展开侧边栏。</p>
            <p>在移动设备上，侧边栏会自动调整为全屏覆盖模式。</p>
        </div>
    </div>

    <script>
        // 等待DOM加载完成
        document.addEventListener('DOMContentLoaded', function() {
            // 获取DOM元素
            const sidebar = document.querySelector('.sidebar');
            const toggleBtn = document.getElementById('toggleBtn');
            const mobileToggleBtn = document.getElementById('mobileToggleBtn');
            const mainContent = document.getElementById('mainContent');
            const menuItems = document.querySelectorAll('.menu-item');
            
            // 切换侧边栏状态
            function toggleSidebar() {
                sidebar.classList.toggle('collapsed');
                
                // 更新按钮图标
                if (sidebar.classList.contains('collapsed')) {
                    toggleBtn.textContent = '▶';
                } else {
                    toggleBtn.textContent = '◀';
                }
            }
            
            // 绑定按钮点击事件
            toggleBtn.addEventListener('click', toggleSidebar);
            mobileToggleBtn.addEventListener('click', toggleSidebar);
            
            // 为菜单项添加点击事件
            menuItems.forEach(item => {
                item.addEventListener('click', function() {
                    // 移除所有active类
                    menuItems.forEach(i => i.classList.remove('active'));
                    // 为当前点击项添加active类
                    this.classList.add('active');
                    
                    // 如果是移动设备且侧边栏展开状态，点击后自动折叠
                    if (window.innerWidth <= 768 && !sidebar.classList.contains('collapsed')) {
                        toggleSidebar();
                    }
                });
            });
            
            // 响应式处理：窗口大小改变时调整侧边栏
            window.addEventListener('resize', function() {
                if (window.innerWidth > 768) {
                    // 桌面设备，确保侧边栏可见
                    sidebar.classList.remove('collapsed');
                    toggleBtn.textContent = '◀';
                }
            });
            
            // 初始化检查
            if (window.innerWidth <= 768) {
                // 移动设备，默认折叠侧边栏
                sidebar.classList.add('collapsed');
                toggleBtn.textContent = '▶';
            }
        });
    </script>
</body>
</html>
